package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.RoleInfoEntity;
import com.cheng.data.entity.RolePermEntity;
import com.cheng.data.mapper.RolePermMapper;
import com.cheng.service.RolePermService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ChengJW
 * @date: 2021/11/17 7:19
 * @version: 1.0
 * @description: 角色-权限表
 */
@Service
public class RolePermServiceImpl implements RolePermService {

    private final static Logger log = LoggerFactory.getLogger(RolePermServiceImpl.class);

    @Autowired
    private RolePermMapper rolePermMapper;

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/11/17
     * @description: 新增角色-权限信息
     */
    @Override
    public int insertRolePerm(RolePermEntity entity) throws CommonException {
        return rolePermMapper.insert(entity);
    }

    /**
    * @author: ChengJW
    * @params: [list]
    * @return: int
    * @date: 2021/11/17
    * @description: 批量插入角色-权限信息
    */
    @Override
    public int insertBatchRolePerm(List<RolePermEntity> list) throws CommonException {
        if (null != list && list.size() > 0){
            for (RolePermEntity entity : list){
                insertRolePerm(entity);
            }
        }
        return 0;
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/11/17
     * @description: 修改角色-权限信息
     */
    @Override
    public int updateRolePerm(RolePermEntity entity) throws CommonException {
        return rolePermMapper.updateById(entity);
    }

    /**
     * @author: ChengJW
     * @params: [list]
     * @return: int
     * @date: 2021/11/18
     * @description: 批量修改角色-权限信息
     */
    @Override
    public int updateBatchRolePerm(List<RolePermEntity> list) throws CommonException {

        if(null != list && list.size()> 0){
            for (RolePermEntity entity : list){
                //先根据id删除，再插入
                deleteRolePerm(entity);
                insertRolePerm(entity);
            }
        }
        return 0;
    }

    /**
     * @author: ChengJW
     * @params: [entity]
     * @return: int
     * @date: 2021/11/17
     * @description: 删除角色-权限信息
     */
    @Override
    public int deleteRolePerm(RolePermEntity entity) throws CommonException {
        return rolePermMapper.deleteById(entity.getRpId());
    }

    /**
    * @author: ChengJW
    * @params: [entity]
    * @return: int
    * @date: 2021/11/18
    * @description: 根据角色 ID 删除角色-权限信息
    */
    @Override
    public int deleteBatchRolePerm(RolePermEntity entity) throws CommonException {

        QueryWrapper<RolePermEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtil.isNotNull(entity.getRoId()),"ro_id",entity.getRoId());
        return rolePermMapper.delete(wrapper);
    }
}
